package com.liar.modular.user.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liar.base.BaseController;
import com.liar.base.ResultModel;
import com.liar.modular.user.model.QueryInfo;
import com.liar.modular.user.model.UserInfo;
import com.liar.modular.user.service.UserInfoService;
import com.liar.utils.common.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "http://127.0.0.1:8080", maxAge = 3600)
@RestController
public class LoginController extends BaseController {

    @Autowired
    private UserInfoService userInfoService;
    /**
     * 登录接口
     */
    @RequestMapping(value = "login", method = RequestMethod.POST ,produces = "text/json;charset=UTF-8")
    public String login(@RequestBody String json){
      logBefore(logger,"登录"+json);
        ResultModel resultModel = new ResultModel();
        UserInfo userInfo = JSON.parseObject(json, UserInfo.class);
        UserInfo login = userInfoService.login(userInfo);
        if(EmptyUtil.isNotEmpty(login)){
            resultModel.setStatus("200");
            resultModel.setSuccess(true);
            resultModel.setData(login);
            resultModel.setToken("fsdfdsfsdfgdfghdfgdfngjkdfghnduifg");
            resultModel.setMessage("登录成功");
        }else {
            resultModel.setStatus("201");
            resultModel.setSuccess(false);
            resultModel.setMessage("登录失败");
        }
        return JSON.toJSONString(resultModel);
    }
    /**
     * 获取用户列表
     */
    @RequestMapping(value = "userList", method = RequestMethod.POST, produces = "text/json;charset=UTF-8")
    public String userList(@RequestBody String json){
        logBefore(logger,"用户列表"+json);
        ResultModel resultModel = new ResultModel();
        QueryInfo queryInfo = JSON.parseObject(json, QueryInfo.class);
        Page<UserInfo> userInfoPage = userInfoService.userList(queryInfo);
        List<UserInfo> records = userInfoPage.getRecords();
        resultModel.setTotalPage(userInfoPage.getTotal());
        resultModel.setData(records);
        resultModel.setStatus("200");
        return JSON.toJSONString(resultModel);
    }
    /**
     * 修改用户状态
     */
    @RequestMapping(value = "state",method = RequestMethod.POST,produces = "text/json;charset=UTF-8")
    public String state(@RequestBody String json){
        logBefore(logger,"修改用户状态"+json);
        ResultModel resultModel = new ResultModel();
        UserInfo userInfo = JSON.parseObject(json, UserInfo.class);
        boolean state = userInfoService.state(userInfo);
        if(state){
            resultModel.setStatus("200");
        }else {
            resultModel.setStatus("201");
        }
        return JSON.toJSONString(resultModel);
    }
    /**
     * 添加用户
     */
    @RequestMapping(value = "addUser", method = RequestMethod.POST, produces = "text/json;charset=UTF-8")
    public String addUser(@RequestBody String json){
        logBefore(logger,"添加用户"+json);
        ResultModel resultModel = new ResultModel();
        UserInfo userInfo = JSON.parseObject(json, UserInfo.class);
        boolean b = userInfoService.addUser(userInfo);
        if(b){
            resultModel.setStatus("200");
        }else {
            resultModel.setStatus("201");
        }
        return JSON.toJSONString(resultModel);
    }
    /**
     * 用户修改
     */
    @RequestMapping(value = "toEdit/{id}",method = RequestMethod.GET,produces = "text/json;charset=UTF-8")
    public String toEdit(@PathVariable String id){
        logBefore(logger,"用户修改"+id);
        ResultModel resultModel = new ResultModel();
        UserInfo userInfo = userInfoService.getById(id);
        if(EmptyUtil.isNotEmpty(userInfo)){
            resultModel.setStatus("200");
            resultModel.setData(userInfo);
        }else {
            resultModel.setStatus("201");
        }
       return JSON.toJSONString(resultModel);
    }

}
